<?php
/**
 * Created by PhpStorm.
 * User: maple
 * Date: 17-11-24
 * Time: 下午4:15
 */

namespace AppBundle\Services;

use AppBundle\Exception\ServiceException;
use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\PDOConnection;
use Psr\Log\LoggerInterface;

class LogService extends BaseService
{
    /** @var  PDOConnection */
    private $dbconn;

    /**
     * DB2Service constructor.
     * @param Connection $dbconn
     * @param LoggerInterface $logger
     */
    public function __construct($dbconn, $logger)
    {
        $this->dbconn = $dbconn;
        $this->setLogger($logger);
    }

    /**
     * 初始化Log对象
     * @param $params
     * @return bool|string
     * @throws \Exception
     */
    public function initLog($params)
    {
        try {
            $this->dbconn->beginTransaction();
            $sql = 'INSERT INTO TZ_CGB_BMCRM_LOG 
                    (START_TIME, REQUEST_MSG)
                    VALUES (:startTime, :requestMsg)';

            $sth = $this->dbconn->prepare($sql);

            foreach ($params as $key => $param) {
                $sth->bindValue($key, $param);
            }
            $result = $sth->execute();
            if (!$result) {
                $this->logError('[LogService initLog] Execute sql failed, error code: ' . $sth->errorCode());
                return false;
            }

            $res = $this->dbconn->lastInsertId('ROW_ID');
            $this->logInfo('[LogService initLog] Log success.[' . $res . ']');
            $this->dbconn->commit();
            return $res;
        } catch (\Exception $ex) {
            $this->dbconn->rollBack();
            $this->logError('[LogService initLog] Has exception, message: ' . $ex->getMessage());
            throw $ex;
        }
    }

    public function contentLog($params)
    {
        try {
            $this->dbconn->beginTransaction();
            $sql = 'UPDATE TZ_CGB_BMCRM_LOG 
                    SET SENDER_ID=:senderId, SENDER_SN=:senderSN, TRADE_CODE=:tradeCode, REQUEST_TIME=:requestTime, LAST_UPD=CURRENT TIMESTAMP
                    WHERE ROW_ID=:rowId';

            $sth = $this->dbconn->prepare($sql);

            foreach ($params as $key => $param) {
                $sth->bindValue($key, $param);
            }
            $result = $sth->execute();
            if (!$result) {
                $this->logError('[LogService contentLog] Execute sql failed, error code: ' . $sth->errorCode());
                return false;
            }

            $res = $sth->rowCount();
            $this->logInfo('[LogService contentLog] Log success.[' . $res . ']');
            $this->dbconn->commit();
            return $res;
        } catch (\Exception $ex) {
            $this->dbconn->rollBack();
            $this->logError('[LogService contentLog] Has exception, message: ' . $ex->getMessage());
            throw new ServiceException($ex->getMessage(), '', [], $ex->getCode());
        }
    }

    public function resultLog($params)
    {
        try {
            $this->dbconn->beginTransaction();
            $sql = 'UPDATE TZ_CGB_BMCRM_LOG 
                    SET LAST_UPD=CURRENT TIMESTAMP, END_TIME=:endTime, ERROR_CODE=:errorCode, ERROR_MSG=:errorMsg, RESPONSE_MSG=:responseMsg
                    WHERE ROW_ID=:rowId';

            $sth = $this->dbconn->prepare($sql);

            foreach ($params as $key => $param) {
                $sth->bindValue($key, $param);
            }
            $result = $sth->execute();
            if (!$result) {
                $this->logError('[LogService resultLog] Execute sql failed, error code: ' . $sth->errorCode());
                return false;
            }

            $res = $sth->rowCount();
            $this->logInfo('[LogService resultLog] Log success.[' . $res . ']');
            $this->dbconn->commit();
            return $res;
        } catch (\Exception $ex) {
            $this->dbconn->rollBack();
            $this->logError('[LogService resultLog] Has exception, message: ' . $ex->getMessage());
            throw $ex;
        }
    }
}